package com.lookme.lmtool.utils;

import cn.hutool.setting.Setting;
import com.lookme.lmtool.model.Env;
import com.lookme.lmtool.model.ProjectInfo;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 环境操作工具类
 */
public class EnvUtils {
    public static ProjectInfo loadProject(String path){
        ProjectInfo projectInfo =null;
        try {
            projectInfo=(ProjectInfo) SerializationUtils.readFromXmlFile(path);
            projectInfo.setPath(path);
            Env.getInstance().setLastOpenProject(path);
        }catch(Exception e){
            projectInfo=new ProjectInfo();
            projectInfo.setPath(path);
        }
        if(projectInfo.getItemInfoList()==null){
            projectInfo.setItemInfoList(new ArrayList<>());
        }
        return projectInfo;
    }

    public static ProjectInfo loadProject(InputStream stream){
        ProjectInfo projectInfo =null;
        try {
            projectInfo=(ProjectInfo) SerializationUtils.readFromXmlStream(stream);
            projectInfo.setPath(null);
            Env.getInstance().setLastOpenProject(null);
        }catch(Exception e){
            projectInfo=new ProjectInfo();
            projectInfo.setPath(null);
        }
        if(projectInfo.getItemInfoList()==null){
            projectInfo.setItemInfoList(new ArrayList<>());
        }
        return projectInfo;
    }

    public static void saveProject(ProjectInfo projectInfo){
        try {
            SerializationUtils.saveToFileAsXml(projectInfo,projectInfo.getPath());
            Env.getInstance().setLastOpenProject(projectInfo.getPath());
            Env.getInstance().addHistoryProject(projectInfo.getPath());
            saveSetting();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void loadSetting(){
        try {
            Setting setting = new Setting("captch_cracker.properties");
            String path = setting.getStr("lastOpenProject");
            File file=new File(path);
            if(file.exists()){
                Env.getInstance().setLastOpenProject(path);
            }

            List<String >historyProjects=new ArrayList<>();
            if (setting.getStr("historyProjects") != null) {
                List<String> history = Arrays.asList(setting.getStr("historyProjects").split(","));
                for(String hpath:history){
                    file=new File(hpath);
                    if(file.exists()){
                        historyProjects.add(file.getAbsolutePath());
                    }
                }
            }
            Env.getInstance().setHistoryProjects(historyProjects);

            path = setting.getStr("initDirectory");
            file=new File(path);
            if(file.exists()){
                Env.getInstance().setInitDirectory(path);
            }
        }catch(Exception e){
            Env.getInstance().setHistoryProjects(new ArrayList<>());
        }
    }

    public static void saveSetting(){
        Setting setting = new Setting();
        setting.set("lastOpenProject", Env.getInstance().getLastOpenProject());
        setting.put("historyProjects", StringUtils.join(Env.getInstance().getHistoryProjects(), ","));
        setting.put("initDirectory",  Env.getInstance().getInitDirectory());
        setting.store("captch_cracker.properties");
    }
}
